package com.eshop.product.service.impl;

import com.eshop.product.rabbitmq.RabbitMQSender;
import com.eshop.product.rabbitmq.RabbitQueue;
import com.eshop.product.service.ProductSpecificationService;
import com.eshop.product.mapper.ProductSpecificationMapper;
import com.eshop.product.model.ProductSpecification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName CategoryServiceImpl
 * @Description
 * @Author 47980
 * @Date 2020/5/17 16:14
 * @Version V_1.0
 **/
@Service
public class ProductSpecificationServiceImpl implements ProductSpecificationService {

    @Autowired
    private ProductSpecificationMapper productSpecificationMapper;
    @Autowired
    private RabbitMQSender rabbitMQSender;

    @Override
    public void add(ProductSpecification productSpecification, String operationType) {
        productSpecificationMapper.add(productSpecification);
        String queue = null;
        if (operationType == null || "".equals(operationType)) {
            queue = RabbitQueue.DATA_CHANGE_QUEUE;
        } else if ("refresh".equals(operationType)) {
            queue = RabbitQueue.REFRESH_DATA_CHANGE_QUEUE;
        } else if ("high".equals(operationType)) {
            queue = RabbitQueue.HIGH_DATA_CHANGE_QUEUE;
        }
        rabbitMQSender.send(queue, "{\"event_type\": \"add\", \"data_type\": \"product_specification\", \"id\": \""+productSpecification.getId()+"\", \"product_id\": "+productSpecification.getProductId()+"}");
    }

    @Override
    public void update(ProductSpecification productSpecification, String operationType) {
        productSpecificationMapper.update(productSpecification);
        String queue = null;
        if (operationType == null || "".equals(operationType)) {
            queue = RabbitQueue.DATA_CHANGE_QUEUE;
        } else if ("refresh".equals(operationType)) {
            queue = RabbitQueue.REFRESH_DATA_CHANGE_QUEUE;
        } else if ("high".equals(operationType)) {
            queue = RabbitQueue.HIGH_DATA_CHANGE_QUEUE;
        }
        rabbitMQSender.send(queue, "{\"event_type\": \"update\", \"data_type\": \"product_specification\", \"id\": \""+productSpecification.getId()+"\", \"product_id\": "+productSpecification.getProductId()+"}");
    }

    @Override
    public void delete(Long id, String operationType) {
        ProductSpecification productSpecification = findById(id);
        productSpecificationMapper.delete(id);
        String queue = null;
        if (operationType == null || "".equals(operationType)) {
            queue = RabbitQueue.DATA_CHANGE_QUEUE;
        } else if ("refresh".equals(operationType)) {
            queue = RabbitQueue.REFRESH_DATA_CHANGE_QUEUE;
        } else if ("high".equals(operationType)) {
            queue = RabbitQueue.HIGH_DATA_CHANGE_QUEUE;
        }
        rabbitMQSender.send(queue, "{\"event_type\": \"delete\", \"data_type\": \"product_specification\", \"id\": \""+id+"\", \"product_id\": "+productSpecification.getProductId()+"}");
    }

    @Override
    public ProductSpecification findById(Long id) {
        return productSpecificationMapper.findById(id);
    }

    @Override
    public ProductSpecification findByProductId(Long productId) {
        return productSpecificationMapper.findByProductId(productId);
    }
}
